package com.yueke.gemini.utils;

import com.google.common.collect.Maps;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.net.URL;
import java.util.Map;

/**
 * created by admin 2020/3/4 16:41
 */
public class DataMapperUtil {
    private static final String path = "mapper/dataMapper.xml";
    private static DataMapperUtil instance = new DataMapperUtil();
    private Map<String,String> dataMapperMap = Maps.newHashMap();

    /**
     * 初始化
     */
    public DataMapperUtil() {
        init();
    }

    public static DataMapperUtil getInstance(){
        return instance;
    }

    private void init(){
        try {
            URL url = DataMapperUtil.class.getClassLoader().getResource(path);
            String path = url.getPath();

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document document = builder.parse(path);

            NodeList nodeList = document.getElementsByTagName("property");

            for (int i = 0 ;i < nodeList.getLength();i++){
                Element node = (Element)nodeList.item(i);
                dataMapperMap.put(node.getAttribute("name"),node.getFirstChild().getNextSibling().getNodeValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public String getData(String key){
        return dataMapperMap.get(key);
    }


    public static void main(String[] args) {
        String data = DataMapperUtil.getInstance().getData("t_user");
        System.out.println(data);
    }
}
